<template>
  <div class="page-box">
    <div class="search-box">
      <el-form ref="formRef" :inline="true" :model="filterOption">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="filterOption.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="filterOption.sex" placeholder="性别">
            <el-option v-for="item in sexDict.arrData" :key="item.id" :label="item.name" :value="item.val"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch"> <search theme="outline" size="14" /> 查询 </el-button>
          <el-button @click="onReset"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="action-box">
      <el-button type="primary" @click="addRow"> <add theme="outline" size="14" /> 新增 </el-button>
      <el-button type="primary" @click="editRow"> <edit theme="outline" size="14" /> 修改 </el-button>
      <el-button type="danger" @click="deleteRow"> <delete theme="outline" size="14" /> 删除 </el-button>
    </div>
    <div class="table-box">
      <el-table
        ref="singleTableRef"
        v-loading="loading"
        height="100%"
        :data="tableData"
        border
        highlight-current-row
        style="width: 100%"
        @current-change="handleCurrentChange"
      >
        <el-table-column label="" width="70">
          <template #default="scope">
            {{ scope.$index + 1 + (page.currentPage - 1) * page.pageSize }}
          </template>
        </el-table-column>
        <el-table-column prop="inspectionDate" label="日期" width="180" />
        <el-table-column prop="userName" label="用户名" width="180" />
        <el-table-column prop="cityName" label="城市" />
        <el-table-column prop="payType" label="支付方式">
          <template #default="scope">
            {{ testDict.jsonData[scope.row.payType]?.name }}
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" show-overflow-tooltip />
        <el-table-column label="操作" align="center" width="100">
          <template #default="scope">
            <el-button type="primary" link @click="showData(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-box">
      <DuPagination :page="page"></DuPagination>
    </div>
  </div>
  <AddDialog ref="addDialogRef"></AddDialog>
  <EditDialog ref="editDialogRef"></EditDialog>
</template>
<script setup>
import { getTableApi } from '@/api/service/m1'
import { sexDict, testDict } from '@/dict/testDict.js'
import { Add, Edit, Delete, Search } from '@icon-park/vue-next'
import useTableData from '@/hooks/common/useTableData.js'
import { useSingleElTable } from '@/hooks/element/useEleTable.js'

import AddDialog from './dialog/AddDialog.vue'
import EditDialog from './dialog/EditDialog.vue'

const formRef = ref(null)
const addDialogRef = ref(null)
const editDialogRef = ref(null)
const filterOption = reactive({
  name: '',
  sex: '',
})
const { loading, page, tableData, getTableData } = useTableData(getTableApi, filterOption)
const { currentRow, handleCurrentChange, validateCurrentRow } = useSingleElTable()

// 查看单行数据
const showData = (data) => {
  currentRow.value = data
  DuMessage.info(currentRow.value)
}

// 查询
const onSearch = () => {
  page.currentPage = 1
  page.start = 0
  getTableData()
}
// 重置
const onReset = () => {
  formRef.value.resetFields()
  getTableData()
}
// 新增
const addRow = () => {
  addDialogRef.value.openDialog()
}
// 修改
const editRow = () => {
  validateCurrentRow()
    .then(() => {
      DuMessage.info(currentRow.value)
      editDialogRef.value.openDialog()
    })
    .catch((e) => {
      console.log(e)
    })
}
// 删除
const deleteRow = () => {
  validateCurrentRow()
    .then(() => {
      DuMessage.info(currentRow.value)
    })
    .catch((e) => {
      console.log(e)
    })
}
</script>

<style scoped lang="scss">
.search-box {
  //margin-bottom: 20px;
  display: flex;
}
.action-box {
  margin-bottom: 10px;
  text-align: right;
}
.table-box {
  flex: 1;
  overflow: hidden;
}
</style>
